// const util = require('../../utils/util.js')
const app = getApp()
const bodyFatData = require('./bodyFatData.js')
Page({
  isLoading: false,
  isEnd:false,
  data: {
    weightRecords:[]
  },
  onLoad: function () {
    
  },
  onShow: function () {
    bodyFatData.setUserInfo(app.globalData.user);
    this.getWeightRecords()
  },
  // 下拉刷新  
  onReachBottom: function () {
    // 显示导航栏loading  
    wx.showNavigationBarLoading();
    // 调用接口加载数据  
    this.getWeightRecords();
    // 隐藏导航栏loading  
    wx.hideNavigationBarLoading();
    // 当处理完数据刷新后，wx.stopPullDownRefresh可以停止当前页面的下拉刷新  
    wx.stopPullDownRefresh();
  },
  onHide: function(){
    
  }, 
  getWeightRecords: function (){
    console.log("getWeightRecord")
    if (this.isLoading || this.isEnd){
      return
    }
    this.isLoading = true
    let last_time = ''
    console.log(this.data.weightRecords)
    console.log(this.data.weightRecords.length)
    if (this.data.weightRecords.length > 0){
      last_time = this.data.weightRecords[this.data.weightRecords.length - 1].updated_at
    }
    wx.request({
      url: app.globalData.api_domain + 'min-pro/weight-records',
      method: 'GET',
      data: {
        openid: app.globalData.user.openid,
        last_time: last_time
      },
      header: {
        'token': app.globalData.user.token // 默认值
      },
      success: (res) => {
        console.log("getWeightRecord", res);
        if (res.statusCode == 200) {
          if (res.data.apistatus == 1) {
            let weightRecords = res.data.result
            if (weightRecords.length <= 0){
              this.isEnd = true
            }else{
              for (let i = 0; i < weightRecords.length; i++) {
                weightRecords[i] = {
                  ...weightRecords[i], fatRateEvaluate: bodyFatData.setFatRate(weightRecords[i].fat_rate),
                  weightRateEvaluate: bodyFatData.setBmi(weightRecords[i].weight)
                }
              }
              this.setData({ weightRecords: this.data.weightRecords.concat(weightRecords) })
            }
          }
        }
        this.isLoading = false
      }
    })
  },
})
